//
//  led.c
//  electronic_clock
//
//  Created by fukai on 2018/10.
//  Copyright © 2018年 赛博智能车实验室. All rights reserved.
//

#include "led.h"
#include "stm32f10x.h"



FlowLampStatusNode FlowLampStatus = LED1_ON;

//LED初始化
void LedInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag，使能SWD，可以用SWD模式调试
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = LED1 | LED2 | LED3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(LED_GPIO,&GPIO_InitStructure);
	GPIO_ResetBits(LED_GPIO,LED1 | LED2 | LED3);
}


//流水灯启动
//用于中断中
//times:中断计数
void FlowLampOn(int times)
{
	static int t = 0;
	
	t++;
	if(t >= times)
	{
		t = 0;
		FlowLampStatus++;
		if(FlowLampStatus > LED3_ON)
		{
			FlowLampStatus = LED1_ON;
		}
	}
	
	switch(FlowLampStatus)
	{
		case LED1_ON:
			GPIO_SetBits(LED_GPIO,LED1);
			GPIO_ResetBits(LED_GPIO,LED2 | LED3);
			break;
		case LED2_ON:
			GPIO_SetBits(LED_GPIO,LED2);
			GPIO_ResetBits(LED_GPIO,LED1 | LED3);
			break;
		case LED3_ON:
			GPIO_SetBits(LED_GPIO,LED3);
			GPIO_ResetBits(LED_GPIO,LED1 | LED2);
			break;
	}
}

//流水灯关闭
void FlowLampOff(void)
{
	GPIO_ResetBits(LED_GPIO,LED1 | LED2 | LED3);
}

//LED灯闪烁
//用于中断中
//times:中断计数
void LedFlicker(int times)
{
	static int t = 0;
	
	t++;
	if(t == times)
	{
		GPIO_SetBits(LED_GPIO,LED1 | LED2 | LED3);
	}
	else if(t == 2*times)
	{
		t = 0;
		GPIO_ResetBits(LED_GPIO,LED1 | LED2 | LED3);
	}
}

//LED灯打开
void LedOn(void)
{
	GPIO_SetBits(LED_GPIO,LED1 | LED2 | LED3);
}

//LED灯关闭
void LedOff(void)
{
	GPIO_ResetBits(LED_GPIO,LED1 | LED2 | LED3);
}


